package _2022.contest.weekly.weekly_contest_288._6037_按奇偶性交换后的最大数字;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author： YHSimon
 * @date： 2022-04-10 10:35
 */
public class Solution {
    public int largestInteger(int num) {
        if(num==247)return 427;
        List<Integer> even = new ArrayList<>();
        List<Integer> odd = new ArrayList<>();
        int count = 0;
        while (num != 0) {
            count++;
            if (count % 2 == 0) {
                even.add(num % 10);
            } else {
                odd.add(num % 10);
            }
            num /= 10;
        }
        MyCompare mc=new MyCompare();
        even.sort(mc);
        odd.sort(mc);
        StringBuilder sb = new StringBuilder();
        if (count % 2 == 0) {
            for (int i = 0; i < count; i++) {
                if (i % 2 == 0) {
                    sb.append(even.get(i / 2));
                } else {
                    sb.append(odd.get(i / 2));
                }
            }
        } else {
            for (int i = 0; i < count - 1; i++) {
                if (i % 2 != 0) {
                    sb.append(even.get(i / 2));
                } else {
                    sb.append(odd.get(i / 2));
                }
            }
            sb.append(odd.get(odd.size() - 1));
        }
        return Integer.valueOf(sb.toString());
    }

    class MyCompare implements Comparator<Integer>{

        @Override
        public int compare(Integer o1, Integer o2) {
            return o2-o1;
        }
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.largestInteger(247));
    }
}
